package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Fragment;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.badibadi.activity.MyDataActivity;
import com.badibadi.activity.PinyinSeeOtherPeopleFriendActivity;
import com.badibadi.activity.SeeOtherPeopleSpaceActivity;
import com.badibadi.infos.Invite_Friend_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.google.zxing.oned.rss.FinderPattern;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

/**
 * 好友验证 Invite_friends_display_time
 * 
 * @author Administrator Invite_friends_common
 * 
 */
public class AfterLandingAddFriendsMsgItemFragment0 extends BaseAppFragment implements
		IXListViewListener {
	// TODO Auto-generated method stub
	private XListView1Adapter____1 x1Adapter;
	private XListView xListView1;
	private int Page;
	private Results results, results2;
	private List<Invite_Friend_Model> friend_Models, list;
	private String uid;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(getActivity());
				
				try {
					onLoad();
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));//网络连接失败
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}

				break;
			case 2:
				Utils.ExitPrgress(getActivity());
				try {
					friend_Models.addAll(list);
					x1Adapter.notifyDataSetChanged();
					onLoad();
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				break;
			case 3:
				Utils.ExitPrgress(getActivity());
				
				try {
					onLoad();
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_xa10));//已经没有更多咯
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
				break;
			case 4:
				Utils.ExitPrgress(getActivity());
				try {
					Utils.showMessage(getActivity(), results2.getRetmsg());

				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		friend_Models = new ArrayList<Invite_Friend_Model>();
		uid=Utils.getUid(getActivity());
		x1Adapter = new XListView1Adapter____1();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.after_landing_freinds00_1, null);
		InitMyXListView1(view);
		return view;
	}

	public void InitMyXListView1(View view) {
		Page = 0;
		Constants.Invite_handler = mHandler;
		xListView1 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView1.setXListViewListener(this);
		xListView1.setPullLoadEnable(true);
		xListView1.setPullRefreshEnable(true);
		xListView1.setAdapter(x1Adapter);
		xListView1.startLoadMore();
	}

	private void Invite_announce(final int page) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<Invite_Friend_Model>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("type", 1);
				map.put("page", page);
				map.put("pageNum", 10);
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "invite_announce");
				if (invite_content != null) {
					results = Utils.checkResult_NNN(getActivity(),
							invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   ("解析出来的数据:" + results.getRetmsg());
//						   
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(),
									Invite_Friend_Model.class);
							mHandler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						mHandler.sendEmptyMessage(3);
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					mHandler.sendEmptyMessage(1);
				}

			}
		});
	}

	protected void onLoad() {
		xListView1.stopRefresh();
		xListView1.stopLoadMore();
		xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
	}

	class XListView1Adapter____1 extends BaseAdapter {
		private DisplayImageOptions options;

		public XListView1Adapter____1() {
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_stub)
					.showImageForEmptyUri(R.drawable.noimage)
					.showImageOnFail(R.drawable.noimage).cacheInMemory()
					.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
					.build();
		}

		@Override
		public int getCount() {

			return friend_Models.size();
		}

		@Override
		public Object getItem(int position) {
			return friend_Models.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			convertView = inflater.inflate(
					R.layout.fragment_afterlanding_friends_import_layout, null);
			ImageView Invite_friends_back_img = (ImageView) convertView
					.findViewById(R.id.Invite_friends_back_img);
			final ImageView Invite_friends_add = (ImageView) convertView
					.findViewById(R.id.Invite_friends_add);
			final ImageView Invite_friends_no_add = (ImageView) convertView
					.findViewById(R.id.Invite_friends_no_add);
			TextView Invite_friends_name = (TextView) convertView
					.findViewById(R.id.Invite_friends_name);
			TextView Invite_friends_message = (TextView) convertView
					.findViewById(R.id.Invite_friends_message);
			TextView Invite_friends_common = (TextView) convertView
					.findViewById(R.id.Invite_friends_common);
			TextView Invite_friends_display_time = (TextView) convertView
					.findViewById(R.id.Invite_friends_display_time);
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl
							+ friend_Models.get(position).getHead()
							+ Constants.appPhoto4img, Invite_friends_back_img,
					options);
			Invite_friends_name.setText(friend_Models.get(position)
					.getNickName());
			Invite_friends_back_img
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (friend_Models.get(position).getId() != null) {
								if (!friend_Models
										.get(position)
										.getId()
										.equals(""
												+ Utils.getUid(getActivity()))) {
									Intent intent = new Intent(getActivity(),
											SeeOtherPeopleSpaceActivity.class);
									intent.putExtra("friend_id", friend_Models
											.get(position).getId());
									startActivity(intent);
								} else {
									Intent intent = new Intent(getActivity(),
											MyDataActivity.class);
									// intent.putExtra("friend_id",
									// friend_Models.get(position).getId());
									startActivity(intent);
								}
							}

						}
					});

			Invite_friends_name.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (friend_Models.get(position).getId() != null) {
						if (!friend_Models.get(position).getId()
								.equals("" + Utils.getUid(getActivity()))) {
							Intent intent = new Intent(getActivity(),
									SeeOtherPeopleSpaceActivity.class);
							intent.putExtra("friend_id",
									friend_Models.get(position).getId());
							startActivity(intent);
						} else {
							Intent intent = new Intent(getActivity(),
									MyDataActivity.class);
							// intent.putExtra("friend_id",
							// friend_Models.get(position).getId());
							startActivity(intent);
						}
					}
				}
			});
			Invite_friends_message.setText(friend_Models.get(position)
					.getMessage());
			Invite_friends_common.setText(friend_Models.get(position)
					.getEqual());
			Invite_friends_common
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(getActivity(),
									PinyinSeeOtherPeopleFriendActivity.class);
							intent.putExtra("uid", friend_Models.get(position)
									.getId());
							intent.putExtra("fid", Utils.getUid(getActivity()));
							startActivity(intent);
						}
					});
			// Invite_friends_display_time.setText(friend_Models.get(position)
			// .getRequest_time());
			// Double
			// al=Double.parseDouble(friend_Models.get(position).getRequest_time()+"000"));
			Invite_friends_display_time.setText(CalendarTools
					.getTimeChangeToString(getActivity(),friend_Models.get(position)
							.getRequest_time()));
			if (friend_Models.get(position).getStatus().equals("0")) {
				Invite_friends_add.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Invite_friends_no_add.setVisibility(View.GONE);
						Agree_refuse_firend(position, 2, Invite_friends_no_add);
					}
				});
				Invite_friends_no_add.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Invite_friends_add.setVisibility(View.GONE);
						Agree_refuse_firend(position, 1, Invite_friends_add);
					}
				});
			} else if (friend_Models.get(position).getStatus().equals("1")) {
				Invite_friends_add.setVisibility(View.GONE);
			} else if (friend_Models.get(position).getStatus().equals("2")) {
				Invite_friends_no_add.setVisibility(View.GONE);
			} else {// 意外情况下
				Invite_friends_add.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Invite_friends_no_add.setVisibility(View.GONE);
						Agree_refuse_firend(position, 2, Invite_friends_no_add);
					}
				});
				Invite_friends_no_add.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Invite_friends_add.setVisibility(View.GONE);
						Agree_refuse_firend(position, 1, Invite_friends_add);
					}
				});
			}

			return convertView;
		}

		private void Agree_refuse_firend(final int pos, final int is_agree,
				final ImageView Invite) {
			Utils.showPrgress(getActivity());
			MyThreadTool.fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("uid", Utils.getUid(getActivity()));
					map.put("type", is_agree);
					map.put("msgId", friend_Models.get(pos).getMsgId());
					String agree_content = Utils.sendRequest(map,
							Constants.BadiUrl + "agree_refuse_firend");
					if (agree_content != null) {
						results2 = Utils.checkResult_NNN(getActivity(),
								agree_content);
						if (results2 != null
								&& !results2.getRetmsg().equals("null")
								&& results2.isRet()) {
							Invite.setVisibility(View.GONE);
							Constants.Invite_handler.sendEmptyMessage(4);
						} else {
							Constants.Invite_handler.sendEmptyMessage(3);
						}
					} else {
						Constants.Invite_handler.sendEmptyMessage(1);
					}
				}
			});
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Page = 1;
		friend_Models = new ArrayList<Invite_Friend_Model>();
		Invite_announce(Page);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Invite_announce(++Page);
	}
}